//
//  CMyOrderSearchVC.m
//  ProjectforHDX
//
//  Created by yinda on 16/8/9.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "CMyOrderSearchVC.h"
#import "SearchBar.h"
#import "MainHandleTableCell.h"
#import "DeliveryModel.h"
#import "CSMyOrderConfirmingTVC.h"
#import "OrderDetailsVC.h"

@interface CMyOrderSearchVC ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic , strong)NSMutableArray *plateArray;

@property (nonatomic, strong)UITableView *tableView;

@property(nonatomic,strong)SearchBar *search;

@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation CMyOrderSearchVC

-(NSMutableArray *)faultPars
{
    if (!_tableArray) {
        _tableArray = [NSMutableArray array];
    }
    return  _tableArray;
}



-(void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.search.keyWordText becomeFirstResponder];
}

-(void)requestData:(NSString *)searchKey
{
    if (searchKey == nil) {
        [[BDHUD sharedManager]showBDHUD:self.view title:[UtilCheck language:@"orderlist.input.name.code.sn"]];
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @"1";
    params[@"size"] = @"100";
    
    if (self.type != nil) {
        params[@"type"] = self.type;
    }else{
        params[@"type"] = @"0";
    }
    params[@"searchKey"] = searchKey;
    [AFHttpTool postJsonDatawithUrl:@"ordersStatistical" parameters:[params mutableCopy]
                            success:^(id responseObject) {

                                if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                                    if([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]){
                                        NSArray *data=[responseObject objectForKey:@"data"];
                                        self.tableArray=[UtilCheck arraytoModel:data targetModel:@"OrderModel"];
                                        if(self.tableArray.count<1){

                                        }
                                        
                                        [self.tableView reloadData];
                                    }
                                    else{
                                        [self.tableArray removeAllObjects];
                                        if(self.tableArray.count<1){

                                        }
                                        [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                                    }
                                }
                                else{
                                    //返回数据出问题 弹出服务器返回信息
                                    if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                                    }
                                    else{

                                    }
                                    [self.tableArray removeAllObjects];
                                    if(self.tableArray.count<1){

                                    }
                                }
                            } fail:^(id responseObject, NSError *error) {
                                [self.tableView.header endRefreshing];
                                if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
                                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
                                }else {
                                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
                                }
                            }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupView
{
    [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    SearchBar *searchBar = [[SearchBar alloc]init];
    searchBar.keyWordText.placeholder = [UtilCheck language:@"customer.pleaseInputOrderCode"];
    [self.view addSubview:searchBar];
    self.search = searchBar;
    __weak typeof(searchBar) weakSearch = searchBar;
    searchBar.back = ^{
        [weakSearch.keyWordText resignFirstResponder];
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    searchBar.textBlcok = ^(NSString *text)
    {
        [self requestData:text];
    };
    self.view.backgroundColor = [UIColor blackColor];
    [self.search becomeFirstResponder];
    UITableView *table = [[UITableView alloc]init];
    table.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-60);
    [self.view addSubview:table];
    table.delegate = self;
    table.dataSource = self;
    self.tableView =table;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}


#pragma -mark searchbar的delegate 和 datasource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.tableArray.count;
}
#define CELL_HEIGHT 115
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *orderModel=[self.tableArray objectAtIndex:indexPath.row];
    CGFloat cellHeight;
    if (orderModel.customer.length >15) {
        cellHeight = CELL_HEIGHT + 15;
    }
    else {
        cellHeight = CELL_HEIGHT;
    }
    if(orderModel.open){
        MainHandleTableCell *cell= [[MainHandleTableCell alloc] init];
        CGFloat heigt= cellHeight+[cell createStepView:orderModel].height;
        return heigt;
    }
    else{
        return cellHeight;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainHandleTableCell *cell = (MainHandleTableCell*)[tableView dequeueReusableCellWithIdentifier:@"MainHandleTableCell"];
    if(cell == nil)
    {
        cell = [[MainHandleTableCell alloc] init];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegateVC=self;
    cell.model=[self.tableArray objectAtIndex:indexPath.row];
    cell.Refesh=^(OrderModel* model){
        [self.tableArray setObject:model atIndexedSubscript:indexPath.row];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.search resignFirstResponder];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderModel *model = [self.tableArray objectAtIndex:indexPath.row];
    NSLog(@"订单类型是%@",model.type);
    if (model.orderType) {
        if ([model.orderType integerValue] > 2) {
            OrderDetailsVC *vc = [[OrderDetailsVC alloc]init];
            vc.orderCode = model.orderCode;
            [self dismissViewControllerAnimated:NO completion:^{
                [self.fatherVC.navigationController pushViewController:vc animated:YES];
                
            }];
            
        }else{
            CSMyOrderConfirmingTVC *vc = [[CSMyOrderConfirmingTVC alloc]init];
            vc.orderCode = model.orderCode;
            [self dismissViewControllerAnimated:NO completion:^{
                [self.fatherVC.navigationController pushViewController:vc animated:YES];
                
            }];
        }
        

    }else{
        [Toolobject showTextWithMessage:@"服务器返回的订单类型不存在"];
    }

    
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}



@end
